/*
 * Copyright (C) 2012, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.benchmark;

import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class LEHIGBenchmark {
    private xowl.utils.logging.Logger logger;
    private xowl.interpreter.Repository repo;
    private xowl.lang.owl2.Ontology onto;
    private int index;
    private List<xowl.lang.actions.Select> queries;
    
    public void execute() {
        init();
        load(4);
        execute_query(queries.get(0));
        //load(1);
        //execute_query(queries.get(0));
        //execute_query(queries.get(0));
    }
    
    private void init() {
        logger = new xowl.utils.logging.Logger(Application.class.getName(), org.apache.log4j.Level.WARN);
        xowl.interpreter.Interpreter.config(logger, "../libraries/");
        repo = xowl.interpreter.Interpreter.createRepositoryNativeRDF(logger);
        onto = repo.resolveOntology("http://swat.cse.lehigh.edu/onto/univ-bench.owl");
        
        repo.loadOntology("data/univ-bench.owl", xowl.interpreter.OWL2Syntax.RDFXML);
        queries = repo.loadQueries("data/queries.owl", xowl.interpreter.OWL2Syntax.Functional);
    }
    
    private void load(int size) {
        RDFWriter writer = new RDFWriter(repo, onto);
        edu.lehigh.swat.bench.uba.Generator generator = new edu.lehigh.swat.bench.uba.Generator();
        long begin = java.lang.System.currentTimeMillis();
        generator.start(size, index, 45, writer);
        long end = java.lang.System.currentTimeMillis();
        float span = ((float)(end-begin))/1000;
        index += size;
        java.lang.System.out.println("-- Loading complete:");
        java.lang.System.out.println(Integer.toString(writer.getCounter()) + " OWL2 axioms");
        java.lang.System.out.println(Float.toString(span) + "s");
        java.lang.System.out.println(Float.toString(writer.getCounter() / span) + " axiom/s");
    }
    
    private void execute_query(xowl.lang.actions.Select query) {
        try {
            long begin = java.lang.System.currentTimeMillis();
            repo.select(query);
            long end = java.lang.System.currentTimeMillis();
            float span = ((float)(end-begin))/1000;
            java.lang.System.out.println("-- query: " + Float.toString(span) + "s");
        } catch (xowl.interpreter.XOWLException ex) { }
    }
}
